//
//  ViewController.m
//  MusicK
//
//  Created by 陈尚飞 on 15/11/29.
//  Copyright © 2015年 陈尚飞. All rights reserved.
//

#import "ViewController.h"
#import "PlayerViewController.h"
#import "RotateTransitionDelegate.h"

@interface ViewController ()
@property (nonatomic, strong) UIView *demoView;

/// 提供转场动画代理,提供转场动画的对象
@property (nonatomic, strong) RotateTransitionDelegate *rotateDelegate;
@end

@implementation ViewController

-(RotateTransitionDelegate *)rotateDelegate {

    if (_rotateDelegate == nil) {
        _rotateDelegate = [[RotateTransitionDelegate alloc] init];
    }
    return _rotateDelegate;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    
//    self.demoView = [[UIView alloc]init];
//    self.demoView.backgroundColor = [UIColor whiteColor];
//    //设置定位点, 注意 ,一定要设置完成定位点之后,在设置frame
//    self.demoView.layer.anchorPoint = CGPointMake(0.5, 1.0);
//    //设置视图位置
//    self.demoView.frame = self.view.bounds;
//    
//    [self.view addSubview: self.demoView];
//    
//    UIPanGestureRecognizer *recongnize = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(moveAtcion:)];
//    
//    [self.demoView addGestureRecognizer:recongnize];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if (![segue.destinationViewController isKindOfClass:[PlayerViewController class]]) {
        return;
    }
    
    //设置 modal 转场类型
    
    PlayerViewController *vc = segue.destinationViewController;
    
    vc.modalPresentationStyle = UIModalPresentationCustom;
    
    //设置转场动画代理
    vc.transitioningDelegate = self.rotateDelegate;
    
    
    NSLog(@"come here");

}

-(void)moveAtcion :(UIPanGestureRecognizer *)recognize{
    CGPoint point = [recognize translationInView:self.demoView];
    CGAffineTransform transform = self.demoView.transform;
    
    CGFloat viewAngle = atan2(transform.b, transform.a);
    CGFloat dx = point.x * cos(viewAngle);
    CGFloat dy = point.y * sin(viewAngle);
    NSLog(@"视图夹角 %f", viewAngle);
    CGFloat angle = (dx + dy) / self.view.bounds.size.width;
    //倾斜视图
    transform = CGAffineTransformRotate(transform, angle);
    //平移视图
    transform.tx += 2 * (dx + dy);
    self.demoView.transform = transform;
    [recognize setTranslation:CGPointZero inView:self.demoView];

}
@end
